/**
 * 
 */
package com.wolfpire.info.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.info.dao.CommunicateCooperationDao;
import com.wolfpire.info.dao.CommunicateCooperationDetailDao;
import com.wolfpire.info.model.CommunicateCooperation;
import com.wolfpire.info.model.CommunicateCooperationDetail;
import com.wolfpire.info.service.CommunicateCooperationService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;

/**
 * @author lihd
 *
 */

@Service("communicateCooperationService")
public class CommunicateCooperationServiceImpl extends
		BaseHibernateService<CommunicateCooperation, Long> implements
		CommunicateCooperationService {

	@Override
	public List<CommunicateCooperation> list(CommunicateCooperation communicateCooperation) {
		return this.communicateCooperationDao.list(communicateCooperation);
	}

	@Override
	public void del(Long id) {
		CommunicateCooperation cc = this.communicateCooperationDao.get(id);
		if (null != cc) {
			cc.setDelFlag(Constants.DELETE_FLAG);
			this.communicateCooperationDao.saveOrUpdate(cc);
		}
	}

	@Override
	public Page<CommunicateCooperation> setPageDataList(
			Page<CommunicateCooperation> page,
			CommunicateCooperation filterCommunicateCooperation) {
		return this.communicateCooperationDao.findCommunicateCooperations(page, filterCommunicateCooperation);
	}

	@Override
	public CommunicateCooperation getFullCommunicateCooperation(Long id) {
		CommunicateCooperation cc = this.communicateCooperationDao.get(id);
		CommunicateCooperationDetail filter = new CommunicateCooperationDetail();
		filter.setCommunicateCooperationId(id);
		List<CommunicateCooperationDetail> details = this.communicateCooperationDetailDao.list(filter);
		if (CollectionUtils.isNotEmpty(details)) {
			for (CommunicateCooperationDetail ccd : details) {
				if ("taiwan".equals(ccd.getCatalogKey())) {
					cc.setTaiwanDetail(ccd);
				} else if ("abroad".equals(ccd.getCatalogKey())) {
					cc.setAbroadDetail(ccd);
				}
			}
		}
		return cc;
	}

	@Override
	public void saveOrUpdate(CommunicateCooperation communicateCooperation) {
		this.communicateCooperationDao.saveOrUpdate(communicateCooperation);
		List<CommunicateCooperationDetail> details = new ArrayList<CommunicateCooperationDetail>();
		details.add(communicateCooperation.getTaiwanDetail());
		details.add(communicateCooperation.getAbroadDetail());
		Long id = communicateCooperation.getId();
		this.communicateCooperationDetailDao.deleteByCommunicateCooperationId(id);
		for (CommunicateCooperationDetail ccd : details) {
			ccd.setCommunicateCooperationId(id);
		}
		this.communicateCooperationDetailDao.batchSave(details);
	}

	@Override
	protected IBaseHibernateDao<CommunicateCooperation, Long> getEntityDao() {
		return this.communicateCooperationDao;
	}
	
	@Resource private CommunicateCooperationDao communicateCooperationDao;
	
	@Resource private CommunicateCooperationDetailDao communicateCooperationDetailDao;

	@Override
	public Page<Map<String, Object>> setCCsPageDataList(
			Page<Map<String, Object>> page,
			CommunicateCooperation filterCommunicateCooperation) {
		return this.communicateCooperationDao.findCCs(page, filterCommunicateCooperation);
	}
}
